package com.xiaoxian.basic.util;

import com.xiaoxian.basic.util.jwt.JwtUtils;
import com.xiaoxian.basic.util.jwt.Payload;
import com.xiaoxian.basic.util.jwt.RsaUtils;
import com.xiaoxian.basic.util.jwt.UserInfo;
import com.xiaoxian.user.domain.LoginInfo;

import javax.servlet.http.HttpServletRequest;
import java.security.PublicKey;

public class GetLoginInfoUtil {

    public static LoginInfo getLoginInfo(HttpServletRequest req) throws Exception {
        String header = req.getHeader("U-TOKEN");
        //判断token是否存在
        if(header!=null) {
            //获取redis中的token
            //Object token = redisTemplate.opsForValue().get(header);
            //解密token获取加密的信息
            PublicKey publicKey = RsaUtils.getPublicKey(JwtUtils.class.getClassLoader().getResource("hrm_auth_rsa.pub").getFile());
            Payload<UserInfo> payload = JwtUtils.getInfoFromToken(header, publicKey, UserInfo.class);
            LoginInfo logininfo = payload.getUserInfo().getLoginInfo();
            return logininfo;
        }
        return null;
    }
}
